package com.liyi.service;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.liyi.dao.EmpDao;
import com.liyi.dao.EmpSalaryDao;
import com.liyi.dto.ComStatementDto;
import com.liyi.dto.DepStatementDto;
import com.liyi.dto.EmpStatementDto;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.liyi.dao.DepDao;
import com.liyi.util.ServiceUtil;

@Service("statementService")
@Transactional(rollbackFor = Exception.class)
public class StatementService {

	@Inject
	private EmpSalaryDao empSalaryDaoLiyi;
	@Inject
	private DepDao depDaoLiyi;
	@Inject
	private EmpDao empDaoLiyi;
	
	/* 获取某 [年度] 某 [月度] 公司的 工资统计报表 */
	public ComStatementDto getComStatement(Integer year, Integer month) throws SmsRuntimeException {
		try {
			ComStatementDto re = new ComStatementDto();
			re.setYear(year+"");
			re.setMonth(month+"");
			re.setTotalSalary(empSalaryDaoLiyi.getTotalSalary(year, month)+"");
			re.setAvgSalary(empSalaryDaoLiyi.getAvgSalary(year, month)+"");
			re.setMinSalary(empSalaryDaoLiyi.getMinSalary(year, month)+"");
			re.setMaxSalary(empSalaryDaoLiyi.getMaxSalary(year, month)+"");
			return re;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR, SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}
	
	/* 获取某 [年度] 某 [月度] 某 [部门] 的 工资统计报表 */
	public DepStatementDto getDepStatement(Integer year, Integer month, Integer depId) throws SmsRuntimeException {
		try {
			DepStatementDto statement = new DepStatementDto();
			statement.setDepId(depId+"");
			statement.setDepName(depDaoLiyi.getDepNameByDepId(depId));
			statement.setYear(year+"");
			statement.setMonth(month+"");
			
			String allDeps = ServiceUtil.listIntegerToCommaString(depDaoLiyi.getChildDepsByParentId(depId));
			statement.setTotalBaseSalary(empSalaryDaoLiyi.getDepTotalBaseSalary(year, month, allDeps)+"");
			statement.setAvgBaseSalary(empSalaryDaoLiyi.getDepAvgBaseSalary(year, month, allDeps)+"");
			statement.setMinBaseSalary(empSalaryDaoLiyi.getDepMinBaseSalary(year, month, allDeps)+"");
			statement.setMaxBaseSalary(empSalaryDaoLiyi.getDepMaxBaseSalary(year, month, allDeps)+"");
			return statement;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR, SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}
	
	/* 获取某 [年度] 某 [月度] 某 [员工] 的 工资统计报表 */
	public EmpStatementDto getEmpStatement(Integer year, Integer empId) throws SmsRuntimeException {
		try {
			EmpStatementDto statement = new EmpStatementDto();
			statement.setEmpId(empId+"");
			statement.setYear(year+"");
			statement.setEmpName(empDaoLiyi.getEmpNameByEmpId(empId));
			statement.setTotalBaseSalary(empSalaryDaoLiyi.getEmpTotalBaseSalary(year, empId)+"");
			statement.setTotalHousingFund(empSalaryDaoLiyi.getEmpTotalHousingFund(year, empId)+"");
			statement.setTotalMedical(empSalaryDaoLiyi.getEmpTotalMedical(year, empId)+"");
			statement.setTotalOverworkSalary(empSalaryDaoLiyi.getEmpTotalOverworkSalary(year, empId)+"");
			statement.setTotalPension(empSalaryDaoLiyi.getEmpTotalPension(year, empId)+"");
			statement.setTotalSickleaveHold(empSalaryDaoLiyi.getEmpTotalSickleaveHold(year, empId)+"");
			return statement;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR, SmsErrorConstant.WEB_APPLICATION, "服务器异常");
		}
	}
}
